package cn.fldong.service.impl;

import cn.fldong.dao.CourseDAO;
import cn.fldong.dao.impl.CourseDAOImpl;
import cn.fldong.model.Course;
import cn.fldong.service.CourseService;
import java.sql.SQLException;
import java.util.List;
import java.math.BigDecimal;

/**
 * CourseService接口的实现类，提供课程相关的业务逻辑实现
 */
public class CourseServiceImpl implements CourseService {
    private CourseDAO courseDAO = new CourseDAOImpl();

    @Override
    public Course getCourseById(String courseId) throws SQLException {
        return courseDAO.getCourseById(courseId);
    }

    @Override
    public List<Course> getCoursesByName(String courseName) throws SQLException {
        return courseDAO.getCoursesByName(courseName);
    }

    @Override
    public List<Course> getCoursesBySemester(String semester) throws SQLException {
        return courseDAO.getCoursesBySemester(semester);
    }

    @Override
    public List<Course> getAllCourses() throws SQLException {
        return courseDAO.getAllCourses();
    }

    @Override
    public boolean addCourse(Course course) throws SQLException {
        // 检查课程名称是否已存在
        if (courseNameExists(course.getCourseName())) {
            return false;
        }
        return courseDAO.addCourse(course);
    }

    @Override
    public boolean updateCourse(Course course) throws SQLException {
        // 获取现有课程信息
        Course existingCourse = getCourseById(course.getCourseId());
        if (existingCourse == null) {
            return false; // 课程不存在
        }

        // 检查课程名称是否已存在（排除当前课程）
        if (!existingCourse.getCourseName().equals(course.getCourseName()) && courseNameExists(course.getCourseName())) {
            return false;
        }

        return courseDAO.updateCourse(course);
    }

    @Override
    public boolean deleteCourse(String courseId) throws SQLException {
        // 实际应用中可能需要先检查该课程是否有关联的成绩
        return courseDAO.deleteCourse(courseId);
    }

    @Override
    public boolean courseNameExists(String courseName) throws SQLException {
        return courseDAO.courseNameExists(courseName);
    }
}